package com.wocao.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wocao.entity.ReqBody;
import com.wocao.entity.SysUser;
import com.wocao.mapper.SysUserMapper;
import com.wocao.mapper.SysUserPostMapper;
import com.wocao.mapper.SysUserRoleMapper;
import com.wocao.service.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.*;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.stereotype.Service;
import com.wocao.entity.SysMenu;
import com.wocao.entity.SysRole;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 用户信息表 服务实现类
 */
@Service
@CacheConfig(cacheNames = "sysUser")
public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> implements SysUserService {

    @Autowired
    private SysUserMapper sysUserMapper;

    @Autowired
    private SysUserRoleMapper sysUserRoleMapper;

    @Autowired
    private SysUserPostMapper sysUserPostMapper;

    //分页所有用户
    @Override
    public Page<SysUser> allPage(Page<SysUser> page, ReqBody reqBody) {
        page.getCurrent();
        return sysUserMapper.allPage(page, reqBody);
    }

    //修改用户
    @Override
    @Transactional
    public void updateUser(SysUser sysUser) {
        //添加岗位
        sysUserPostMapper.nullPostByUser(sysUser.getUserId());
        if (sysUser.getPostIds().size() > 0) {
            sysUserPostMapper.addPostByUser(sysUser.getUserId(), sysUser.getPostIds());
        }
        //添加角色
        sysUserRoleMapper.nullRoleByUser(sysUser.getUserId());
        if (sysUser.getRoleIds().size() > 0) {
            sysUserRoleMapper.addRoleByUser(sysUser.getUserId(), sysUser.getRoleIds());
        }
        LocalDateTime upTime = LocalDateTime.now();
        sysUser.setUpdateTime(upTime);
        sysUserMapper.updateById(sysUser);
    }

    //添加用户
    @Override
    @Transactional
    public void addUser(SysUser sysUser) {
        //添加岗位
        if (sysUser.getPostIds().size() > 0) {
            sysUserPostMapper.addPostByUser(sysUser.getUserId(), sysUser.getPostIds());
        }
        //添加角色
        if (sysUser.getRoleIds().size() > 0) {
            sysUserRoleMapper.addRoleByUser(sysUser.getUserId(), sysUser.getRoleIds());
        }
        LocalDateTime createTime = LocalDateTime.now();
        sysUser.setCreateTime(createTime);
        sysUserMapper.insert(sysUser);
    }

    //批量删除
    @Override
    public void batchRemoveUser(Integer[] userIds) throws Exception {
        if (userIds.length > 0) {
            sysUserMapper.batchRemoveUser(userIds);
        } else {
            throw new Exception("ids为空");
        }
    }

    @Override
    public List<GrantedAuthority> getAuthoritiesByUsername(String username) {
        //给用户进行授权
        StringBuilder stringBuilder = new StringBuilder();
        //遍历所有权限名称，连接到字符串中
        List<SysMenu> permissions = sysUserMapper.selectPermissionsByUsername(username);
        permissions.forEach(permission -> {
            stringBuilder.append(permission.getMenuName() + ",");
        });
        //遍历所有角色，加前缀 ROLE_
        List<SysRole> roles = sysUserMapper.selectRolesByUsername(username);
        roles.forEach(role -> {
            stringBuilder.append("ROLE_" + role.getRoleName() + ",");
        });
        //将字符串转换为权限集合
        List<GrantedAuthority> authorities = AuthorityUtils.commaSeparatedStringToAuthorityList(stringBuilder.toString());
        return authorities;
    }

    @Override
    public List<SysMenu> getSysMenusWithChildrenByUsername(String username) {
        return sysUserMapper.selectPermissionsByUsername(username);
    }

    @Override
    public SysUser getPersonalInformationByUsername(String username) {
        return sysUserMapper.selectPersonalInformationByUsername(username);
    }

}
